package leetcode_1000;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 周杨
 * 2018年11月18日 下午12:08:06 返回一个集合 里面的string都是另一个集合里出现的字符
 * WordSubsets_916.java
 * description:hash保存字符即可 AC 90%
 */
public class WordSubsets_916 {
	public static void main(String[] args) {
		WordSubsets_916 test=new WordSubsets_916();
		test.wordSubsets(new String[] {"amazon","apple","facebook","google","leetcode"},
				new String[] {"e","o"});
	}
	
	public List<String> wordSubsets(String[] A, String[] B) {
        int []map=new int[26];
        for(String now:B) {
        	int []m=new int[26];
        	char []chars=now.toCharArray();
        	for(char c:chars) {
        		int diff=c-'a';
        		m[diff]++;
        		map[diff]=Math.max(map[diff], m[diff]);
        	}
        }
        List<String>res=new ArrayList<String>();
        out :for(String now:A) {
        	int []m=new int[26];
        	char []chars=now.toCharArray();
        	for(char c:chars) {
        		int diff=c-'a';
        		m[diff]++;
        	}
        	for(int i=0;i<26;++i) {
        		if(m[i]<map[i])
        			continue out;
        	}
        	res.add(now);
        }
        return res;
    }
}
